package com.jdd.fintech.morse.plugin.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

/**
 * @author: fengcheng
 * @credit: 2021-12-24 15:14
 * @description: json字符串相关工具类
 **/
public class JsonStringUtils {

    /**
     * 日志输出对象
     */
    private static final Logger log = LoggerFactory.getLogger(JsonStringUtils.class);

    /**
     * 判断是否进行json化
     * @param jsonKey
     * @param jsonObject
     * @return
     */
    public static boolean judgeIsJson(String jsonKey, JSONObject jsonObject){
        try {
            if(ObjectUtils.isEmpty(jsonObject)){
                log.error("buildCommonRequestG|相应报文为空|jsonObject : null");
                return false;
            }
            String requestData = jsonObject.getString(jsonKey).trim();
            if(StringUtils.isBlank(requestData)){
                return false;
            }
            if(!requestData.substring(0, 1).equals("{")){
                return false;
            }
        } catch (Exception e) {
            log.error("buildCommonRequestG|数据格式错误|jsonObject : {}", JSON.toJSONString(jsonObject));
            return false;
        }
        return true;
    }
}
